<style lang="less">
@import "@/styles/table-common.less";
.btn-box {
  // padding: 10px 12px 10px 0;
  height: 32px;
  display: flex;
  align-items: center;
  span {
    margin-left: 15px;
  }
}
.detail-modal {
  .form {
    div {
      display: flex;
      margin-bottom: 10px;
      span {
        width: 130px;
      }
      input {
        flex: 1;
        border: 1px solid #e8eaec;
        outline: none;
        text-indent: 16px;
        color: #515a6e;
      }
    }
  }
  .refund-num {
    margin-top: 15px;
  }
}
</style> 
<template>
  <div class="search">
    <Card>
      <Row class="operation">
        <Button @click="add" type="primary" icon="md-add">添加</Button>
      </Row>
      <Form ref="searchForm" :model="searchForm" inline :label-width="100">
        <Row v-show="openSearch" @keydown.enter.native="handleSearch">
          <FormItem label="活动名称：">
            <Input
              type="text"
              v-model="searchForm.title"
              clearable
              placeholder="请输入"
              style="width: 200px"
            />
          </FormItem>

          <FormItem label="活动地点">
            <Input
              type="text"
              v-model="searchForm.siteAddress"
              clearable
              placeholder="请输入"
              style="width: 200px"
            />
          </FormItem>
          <FormItem label="活动状态">
            <Select
              v-model="searchForm.status"
              transfer
              placeholder="请选择"
              style="width: 200px"
            >
              <Option
                v-for="item in statusList"
                :value="item.id"
                :key="item.id"
                >{{ item.title }}</Option
              >
            </Select>
          </FormItem>
          <FormItem style="margin-left: -35px" class="br">
            <Button @click="handleSearch" type="primary" icon="ios-search"
              >搜索</Button
            >
            <Button @click="handleReset">重置</Button>
          </FormItem>
        </Row>
      </Form>
      <Table
        :loading="loading"
        border
        :columns="columns"
        :data="data"
        sortable="custom"
        ref="table"
      >
        <template slot-scope="{ row }" slot="image">
          <!-- <div>{{row.siteValidTimes}}</div> -->
          <div>
            <img
              :src="row.image"
              alt=""
              style="max-width: 160px; max-height: 60px"
            />
          </div>
        </template>

        <template slot-scope="{ row }" slot="activityValidTimes">
          <!-- <div>{{row.siteValidTimes}}</div> -->
          <div v-if="row.activityValidTimes">
            <div v-for="item in row.activityValidTimes" :key="item.id">
              {{ item.validStartDate }}至{{ item.validEndDate }}
            </div>
          </div>
        </template>
        <template slot-scope="{ row }" slot="warmUpStartDate">
          <!-- <div>{{row.siteValidTimes}}</div> -->
          <div v-if="row.warmUpStartDate">
            {{ row.warmUpStartDate }}至{{ row.warmUpEndDate }}
          </div>
        </template>
        <template slot-scope="{ row }" slot="validTime">
          <!-- <div>{{row.siteValidTimes}}</div> -->
          <div v-if="row.startDate">{{ row.startDate }}至{{ row.endDate }}</div>
        </template>
        <template slot-scope="{ row }" slot="type">
          <div>{{ row.type == 1 ? "日常活动" : "党政活动" }}</div>
        </template>
      </Table>
      <Row type="flex" justify="end" class="page">
        <Page
          :current="searchForm.pageNumber"
          :total="total"
          :page-size="searchForm.pageSize"
          @on-change="changePage"
          @on-page-size-change="changePageSize"
          :page-size-opts="[10, 20, 50]"
          size="small"
          show-total
          show-elevator
          show-sizer
        ></Page>
      </Row>
    </Card>
    <addEdit
      :data="formData"
      :type="showType"
      v-model="showUser"
      @on-submit="getDataList"
    />
    <Modal v-model="modal" title="详情" @on-ok="ok">
      <div class="detail-modal">
        <Form label-width="180">
          <FormItem label="处理方式：">
            <Select
              v-model="status"
              transfer
              placeholder="请选择"
              style="width: 200px"
            >
              <Option
                v-for="item in statusList"
                :value="item.id"
                :key="item.id"
                >{{ item.title }}</Option
              >
            </Select>
          </FormItem>
        </Form>
      </div>
    </Modal>
  </div>
</template>

<script>
import {
  scActivityupdStatusById,
  getScActivityByCondition,
  scActivityupdRecommandStatusById,
  scActivityupdShelvesStatusById

} from "@/api/index";

import { shortcuts } from "@/libs/shortcuts";

import addEdit from "./addEdit.vue";
export default {
  name: "user-manage",
  components: {
    addEdit,
  },
  data() {
    return {
      message: "",
      showUser: false,
      showType: "0",
      loading: true,
      selectDate: "",
      openSearch: true,
      status: false,
      modal: false,
      options: {
        shortcuts: shortcuts,
      },
      statusList: [
        { id: "1", title: "未开始" },
        { id: "2", title: "预热" },
        { id: "3", title: "进行中" },
        { id: "4", title: "暂停" },
        { id: "5", title: "结束" },
      ],
      searchForm: {
        pageNumber: 1,
        pageSize: "10",
        sort: "createTime",
        order: "desc",
      },
      //   fixed: "left"
      columns: [
        // {
        //   title: "场地序号",
        //   key: "id",
        //   minWidth: 220,
        //   align: "center",
        // },
        {
          title: "活动名称",
          key: "title",
          width: 220,
          align: "center",
          fixed: "left",
        },
        {
          title: "活动封面",
          slot: "image",
          width: 180,
          align: "center",
        },
        // {
        //   title: "活动类型",
        //   slot: "type",
        //   width: 180,
        //   align: "center",
        // },
        {
          title: "活动地点",
          key: "siteAddress",
          width: 180,
          align: "center",
        },
        {
          title: "活动价格",
          key: "price",
          width: 180,
          align: "center",
        },
        {
          title: "活动人数限制",
          key: "count",
          width: 120,
          align: "center",
        },
        {
          title: "活动日期",
          key: "activityValidTimes",
          slot: "activityValidTimes",
          width: 220,
          align: "center",
        },

        {
          title: "预热时间",
          width: 220,
          align: "center",
          slot: "warmUpStartDate",
        },
        {
          title: "活动时间",
          width: 160,
          align: "center",
          slot: "validTime",
        },
        {
          title: "启用状态",
          key: "status",
          width: 140,
          align: "center",
          fixed: "right",
          render: (h, params) => {
            if (params.row.status == 1) {
              return h("div", [
                h("Badge", {
                  props: {
                    status: "success",
                    text: "未开始",
                  },
                }),
              ]);
            } else if (params.row.status == 2) {
              return h("div", [
                h("Badge", {
                  props: {
                    status: "error",
                    text: "预热",
                  },
                }),
              ]);
            } else if (params.row.status == 3) {
              return h("div", [
                h("Badge", {
                  props: {
                    status: "error",
                    text: "进行中",
                  },
                }),
              ]);
            } else if (params.row.status == 4) {
              return h("div", [
                h("Badge", {
                  props: {
                    status: "error",
                    text: "暂停",
                  },
                }),
              ]);
            } else if (params.row.status == 5) {
              return h("div", [
                h("Badge", {
                  props: {
                    status: "error",
                    text: "结束",
                  },
                }),
              ]);
            }
          },
          // filters: [
          //   {
          //     label: "启用",
          //     value: 0,
          //   },
          //   {
          //     label: "禁用",
          //     value: -1,
          //   },
          // ],
          // filterMultiple: false,
          filterRemote: (e) => {
            let v = "";
            if (e.length > 0) {
              v = e[0];
            }
            this.searchForm.status = v;
            this.searchForm.pageNumber = 1;
            this.getDataList();
          },
        },
        {
          title: "操作",
          key: "createTime",
          width: 220,
          align: "center",
          fixed: "right",
          render: (h, params) => {
            let enableOrDisable = "";
            let enableOrDisable1 = "";
            if (params.row.status != 5) {
              enableOrDisable = h(
                "a",
                {
                  on: {
                    click: () => {
                      this.operation(params.row);
                    },
                  },
                },
                "处理"
              );
            };
              enableOrDisable1 = h(
                "a",
                {
                  on: {
                    click: () => {
                      this.setStatus(params.row);
                    },
                  },
                },
                params.row.shelvesStatus == 1 ? "下架" : "上架"
              );
         
            return h("div", [
           
              enableOrDisable,
                   h("Divider", {
                props: {
                  type: "vertical",
                },
              }),
              enableOrDisable1,
            
              h("Divider", {
                props: {
                  type: "vertical",
                },
              }),
              h(
                "a",
                {
                  on: {
                    click: () => {
                      this.edit(params.row);
                    },
                  },
                },
                "修改"
              ),
              h("Divider", {
                props: {
                  type: "vertical",
                },
              }),
              h(
                "a",
                {
                  on: {
                    click: () => {
                      this.changeRecommand(params.row);
                    },
                  },
                  style: {
                    whiteSpace: "nowrap",
                  },
                },
                params.row.isRecommand == 1 ? "取消推荐" : "推荐至首页"
              ),
            ]);
          },
        },
      ],
      modal: false,
      data: [],
      formData: {},
    };
  },
  methods: {
    setStatus(item){
        //上下架
        this.$Modal.confirm({
          title: "提示",
          content:
            item.shelvesStatus == 0
              ? "确定要修改为上架状态？"
              : "确定要修改为下架状态？",
          onOk: () => {
            scActivityupdShelvesStatusById({
              id: item.id,
              status: item.shelvesStatus == 0 ? 1 : 0,
            }).then((res) => {
              if (res.code == 200) {
                this.$Message.success("修改成功")
                this.getDataList();
                this.$Modal.remove();
              }
            });
          },
          onCancel: () => {
            //  this.$Message.info('Clicked cancel');
          },
        });
    },
    init() {
      this.getDataList();
    },
    changeSort(e) {
      this.searchForm.sort = e.key;
      this.searchForm.order = e.order;
      if (e.order == "normal") {
        this.searchForm.order = "";
      }
      this.getDataList();
    },
    getDataList() {
      // 多条件搜索用户列表
      this.loading = true;
      getScActivityByCondition(this.searchForm).then((res) => {
        this.loading = false;
        if (res.success) {
          this.data = res.result.content;
          this.total = res.result.totalElements;
          if (this.data.length == 0 && this.searchForm.pageNumber > 1) {
            this.searchForm.pageNumber -= 1;
            this.getDataList();
          }
        }
      });
    },

    add() {
      this.showType = "2";
      this.showUser = true;
    },
    edit(v) {
      // 转换null为""
      for (let attr in v) {
        if (v[attr] == null) {
          v[attr] = "";
        }
        if (typeof v[attr] == "number") {
          v[attr] = v[attr].toString();
        }
      }
      let str = JSON.stringify(v);
      let data = JSON.parse(str);
      this.formData = data;
      this.showType = "1";
      this.showUser = true;
    },
    changePage(v) {
      this.searchForm.pageNumber = v;
      this.getDataList();
    },
    changePageSize(v) {
      this.searchForm.pageSize = v;
      this.getDataList();
    },
    selectDateRange(v) {
      if (v) {
        this.searchForm.startDate = v[0];
        this.searchForm.endDate = v[1];
      }
    },
    handleSearch() {
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = 10;
      this.getDataList();
    },
    handleReset() {
      this.selectDate = "";
      // 重新加载数据
      this.searchForm = {
        pageSize: 10,
        pageNumber: 1,
      };
      this.getDataList();
    },
    enable(v) {
      this.$Modal.confirm({
        title: "确认启用",
        content: "您确认要启用用户 " + v.title + " ?",
        loading: true,
        onOk: () => {
          enableUser(v.id).then((res) => {
            this.$Modal.remove();
            if (res.success) {
              this.$Message.success("操作成功");
              this.getDataList();
            }
          });
        },
      });
    },
    chageable(v, status) {
      let statusTxt = status == 0 ? "启用" : "禁用";
      this.$Modal.confirm({
        title: `确认${statusTxt}`,
        content: `您确认要${statusTxt}` + v.title + " ?",
        loading: true,
        onOk: () => {
          scActivityupdStatusById({ id: v.id, status }).then((res) => {
            this.$Modal.remove();
            if (res.success) {
              this.$Message.success("操作成功");
              this.getDataList();
            }
          });
        },
      });
    },
    changeRecommand(v) {
      let statusTxt = v.isRecommand == 0 ? "推荐至首页" : "取消推荐";
      this.$Modal.confirm({
        title: `确认${statusTxt}`,
        content: `您确认要${statusTxt}` + v.title + " ?",
        loading: true,
        onOk: () => {
          scActivityupdRecommandStatusById({
            id: v.id,
            status: v.isRecommand == 0 ? 1 : 0,
          }).then((res) => {
            this.$Modal.remove();
            if (res.success) {
              this.$Message.success("操作成功");
              this.getDataList();
            }
          });
        },
      });
    },
    operation(v) {
      this.modal = true;
      this.formData = v;
      this.status = Number(v.status) + 1 + "";
    },
    cancel() {},
    ok() {
      scActivityupdStatusById({
        id: this.formData.id,
        status: this.status,
      }).then((res) => {
        this.modal = false;
        if (res.success) {
          this.$Message.success("操作成功");
          this.getDataList();
        }
      });
    },
  },
  created() {},
  mounted() {
    // 计算高度

    this.init();
  },
};
</script>